<template>
  <div class="gameboard">
    <div class="canvas">
      <div class="line" v-for="line in datas">
        <div class="block" v-for="item in line" :class="{full: item > 0}"></div>
      </div>
    </div>
    <div class="info">
      <div class="score">Score:{{ score }}</div>
      <div class="next">
        <div class="line" v-for="line in nextblock">
          <div class="block" v-for="item in line" :class="{full: item > 0}"></div>
        </div>
      </div>
      <div class="speed">
        speed: {{ speed }}
      </div>
      <div class="pause">
        <button @click="pause">暂停</button>
      </div>
    </div>
    <div class="gamestart tip-dlg" v-if="gamestart">
      <div class="text">俄罗斯方块</div>
      <div>
        <button @click="start">开始游戏</button>
      </div>
      <div>
        <button>退出</button>
      </div>
    </div>
    <div class="gamepause tip-dlg" v-if="gamepause">
      <div class="text">暂停游戏</div>
      <div>
        <button @click="pause">继续游戏</button>
      </div>
    </div>
    <div class="gameover tip-dlg" v-if="gameover">
      <div class="text">GAMEOVER</div>
      <div class="score">Your Score: {{ score }}</div>
      <div>
        <button @click="start">重新开始</button>
      </div>
    </div>
  </div>
</template>

<script>
import game, {WAIT, PAUSE, RUNNING, GAMEOVER} from '../game'
import { computed, reactive, toRefs, onMounted } from 'vue'
export default {
  name: "gameboard",
  setup() {
    game.init()
    const games = reactive(game)
    const {score, status, speed, next} = toRefs(games)
    const gamestart = computed(() => status.value === WAIT)
    const gamepause = computed(() => status.value === PAUSE)
    const gameover = computed(() => status.value === GAMEOVER)
    const nextblock = computed(() => games.getBlock(next.value, 0))
    const datas = computed(() => games.render())

    onMounted(() => {
      document.onkeydown = (e) => {
        switch(e.code) {
          case 'Space':
            games.trans()
            break
          case 'ArrowLeft':
            games.moveLeft()
            break
          case 'ArrowRight':
            games.moveRight()
            break
          case 'ArrowDown':
            games.down()
            break
        }
      }
    })

    return {
      datas,
      score,
      speed,
      nextblock,
      gamestart,
      gamepause,
      gameover,
      start: () => games.start(),
      pause: () => games.pause(),
    }
  }
}
</script>

<style lang="less">
.block{
  box-sizing:border-box;
  width:40px;
  height:40px;
  border:1px solid #ccc;

  &.full{
    background: #999;
  }
}
.gameboard{
  position:relative;
  margin: 0 auto;
  width: 640px;
  height:100vh;
  border:1px solid #ccc;

  .canvas{
    position: absolute;
    top:0;left:0;bottom:0;
    width:400px;
    height:720px;
    border:3px solid #ccc;

    .line{
      display:flex;
      height: 40px;
    }
  }
  .info{
    position:absolute;
    top:0;right:0;bottom:0;
    width:200px;
    padding:10px;

    .score{
      font-size:24px;
    }

    .next{
      margin-top:10px;

      .line{
        display:flex;
        margin: 0 auto;
        width: 160px;
        height:40px;
      }
    }

    .speed{
      padding: 15px;
    }

    .pause{
      padding: 15px;
    }
  }

  .tip-dlg{
    position:absolute;
    border: 1px solid #ccc;
    padding:15px;
    text-align:center;
    top: 120px;
    left:50%;
    width: 320px;
    margin-left: -160px;
    line-height: 30px;
  }
}
</style>